package com.zfsbswx.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

import com.tool.utils.utils.NetUtils;
import com.tool.utils.utils.SPUtils;
import com.tool.utils.utils.StringUtils;
import com.tool.utils.utils.ToastUtils;
import com.zfsbswx.R;
import com.zfsbswx.common.CommonFunc;
import com.zfsbswx.config.Constants;
import com.zfsbswx.core.action.LoginAction;
import com.zfsbswx.core.myinterface.UiAction;


public class OperatorLoginActivity extends BaseActivity implements OnClickListener, OnTouchListener {

    private Button btnOperatorLogin;
    private EditText edPassWord;

    private LoginAction loginAction;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_operator_login);
//        AppManager.getAppManager().addActivity(this);
        initView();
        initData();
        addListener();
    }


    private void initData() {
        loginAction = new LoginAction(this, uiAction);
    }

    private void initView() {
        btnOperatorLogin = (Button) findViewById(R.id.id_login);
        edPassWord = (EditText) findViewById(R.id.password);
        edPassWord.setText("");
        edPassWord.setSelection(edPassWord.getText().length());
        edPassWord.setOnTouchListener(this);

    }

    private void addListener() {
        btnOperatorLogin.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.id_login:
                login();
                break;
            default:
                break;
        }
    }

    /**
     * 密码框获取焦点
     */
    private void getFocus() {
        edPassWord.postDelayed(new Runnable() {
            @Override
            public void run() {
                edPassWord.setFocusable(true);
                edPassWord.setFocusableInTouchMode(true);
                edPassWord.requestFocus();
                edPassWord.findFocus();
            }
        }, 500);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(edPassWord.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        getFocus();
        return true;
    }


    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
            login();
            return true;
        }
        return super.dispatchKeyEvent(event);
    }


    private UiAction uiAction = new UiAction() {

        @Override
        public void UiResultAction(Activity context, Class<?> cls, Bundle bundle, int requestCode) {
            CommonFunc.startResultAction(context, cls, bundle, requestCode);
        }

        @Override
        public void UiAction(Activity context, Class<?> cls, Bundle bundle, boolean flag) {
            CommonFunc.startAction(context, cls, bundle, flag);
        }

        @Override
        public void UiAction(Activity context, Class<?> cls, boolean flag) {
            CommonFunc.startAction(context, cls, flag);
        }
    };

    private void login() {
        String pass = (String) SPUtils.get(this, Constants.OPERATOR_PASS, Constants.DEFAULT_OPERATOR_PASS);
        if (NetUtils.isConnected(OperatorLoginActivity.this)) {
            if (TextUtils.isEmpty(edPassWord.getText().toString())) {
                ToastUtils.CustomShow(this, "请输入密码");
                getFocus();
                return;
            }
            if (StringUtils.isEquals(pass, edPassWord.getText().toString())) {
                // 判断当天是否再次签到
                if (!CommonFunc.isLogin(this, Constants.SBS_LOGIN_TIME, Constants.DEFAULT_SBS_LOGIN_TIME)) {
                    // 直接进入主界面
                    CommonFunc.startAction(this, SaleMainActivity.class, true);
                    return;
                }
                loginAction.loginAction();
            } else {
                ToastUtils.CustomShow(this, "密码错误");
                getFocus();
            }

        } else {
            ToastUtils.CustomShow(this, "请打开网络");
            getFocus();
        }
    }


//    private void memberTransAmountAction() {
//
//        MemberTransAmountRequest request = new MemberTransAmountRequest();
//        request.setSid(1);
//        request.setMemberCardNo("123456");
//        request.setPassword("123456");
//        request.setTradeMoney(1);
//        request.setPoint(1);
//        request.setCouponSn("123,123,123");
//        request.setMemberName("123456");
//        this.sbsAction.memberTransAmount(this, request, new ActionCallbackListener<MemberTransAmountResponse>() {
//            @Override
//            public void onSuccess(MemberTransAmountResponse data) {
//                ToastUtils.CustomShow(OperatorLoginActivity.this, data.toString());
//            }
//
//            @Override
//            public void onFailure(String errorEvent, String message) {
//                ToastUtils.CustomShow(OperatorLoginActivity.this, message);
//            }
//        });
//    }
//
//    @SuppressLint("NewApi")
//    private void transUploadAction() {
//
//        TransUploadRequest request = new TransUploadRequest();
//        request.setSid(1);
//        request.setCardNo("12233");
//        request.setPassword("123455");
//        request.setCash(1);
//        request.setBankAmount(1);
//        request.setCouponCoverAmount(1);
//        request.setPointCoverAmount(1);
//        request.setCouponSns("12333");
//        request.setClientOrderNo("123222");
//        request.setActivateCode("12344");
//        request.setMerchantNo("12222");
//        request.setT(12111);
//        request.setTransNo("121111");
//        request.setAuthCode("1211");
//        request.setSerialNum(android.os.Build.SERIAL);
//        request.setPayType(1);
//        request.setPointAmount(1);
//
//        this.sbsAction.transUpload(this, request, new ActionCallbackListener<TransUploadResponse>() {
//            @Override
//            public void onSuccess(TransUploadResponse data) {
//                ToastUtils.CustomShow(OperatorLoginActivity.this, data.toString());
//            }
//
//            @Override
//            public void onFailure(String errorEvent, String message) {
//                ToastUtils.CustomShow(OperatorLoginActivity.this, message);
//            }
//        });
//    }
//
//    private void SbsGetPrinterDataAction() {
//        this.sbsAction.getPrinterData(this, 1, "1212", new ActionCallbackListener<TransUploadResponse>() {
//            @Override
//            public void onSuccess(TransUploadResponse data) {
//                ToastUtils.CustomShow(OperatorLoginActivity.this, data.toString());
//            }
//
//            @Override
//            public void onFailure(String errorEvent, String message) {
//                ToastUtils.CustomShow(OperatorLoginActivity.this, message);
//            }
//        });
//    }
//
//    /**
//     * 生成客户端订单号
//     *
//     * @param context
//     * @param payType_in
//     * @return
//     */
//    public static String getNewClientSn(Context context, int payType_in) {
//        String orderNo = "";
//        String device = "2"; // 1:手机 2:Pos机
//        String payType = "" + payType_in; // 交易收款方式
//        String timestamp = StringUtils.getFormatCurTime(); // 时间戳
//        String randomNum = StringUtils.createRandomNumStr(3); // 三位随机数
//        String activateCode = getActivateCode(context, payType_in);
//        orderNo = device + payType + timestamp + randomNum + activateCode;
//        return orderNo;
//    }
//
//    @SuppressLint("NewApi")
//    private static String getActivateCode(Context context, int payType_in) {
//
//        String activateCode = "";
//
//        switch (payType_in) {
//            case Constants.PAY_WAY_ALY:
//            case Constants.PAY_WAY_BFB:
//            case Constants.PAY_WAY_JD:
//            case Constants.PAY_WAY_WX:
//                activateCode += "";
//                break;
//            case Constants.PAY_WAY_FLOT:
//            case Constants.PAY_WAY_CASH:
//                activateCode += android.os.Build.SERIAL;
//                break;
//            default:
//                break;
//        }
//        return activateCode;
//    }

}
